package com.liqin.nothing.ai;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import io.reactivex.Flowable;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * @author gonghl
 * @since 2024/8/12
 */
@Component
public class TongYiClient {

    @Value("${spring.cloud.ai.tongyi.api-key}")
    private static String APIKEY;

    @SneakyThrows
    public Flowable<GenerationResult> sendMessage(String message) {
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(message).build();
        GenerationParam param = GenerationParam.builder()
                .model("qwen-turbo")
                .messages(Collections.singletonList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .incrementalOutput(true)
                .build();
        param.setApiKey(APIKEY);
        Flowable<GenerationResult> flowable = new Generation().streamCall(param);
        return flowable;
    }

}